import 'package:flutter/material.dart';

class StarNotification extends Notification{
  final double index;
  StarNotification({this.index}) : super();
}

class StarClipper extends CustomClipper<Rect>{

  final double width;
  StarClipper({this.width=50});

  @override
  Rect getClip(Size size) {
    // TODO: implement getClip
    return Rect.fromLTRB(0, 0, width, size.height);
  }

  @override
  bool shouldReclip(StarClipper oldClipper) {
    // TODO: implement shouldReclip
    return width != oldClipper.width;
  }

}

class StarItemField extends StatefulWidget {
  final ImageProvider image;
  final Color selected, unselected;
  final double size, select, fill;
  final int index;
  StarItemField({Key key,
    this.index=0,
    this.image,
    this.size,
    this.selected,
    this.unselected,
    this.select=10,
    this.fill=0.6}) : super(key: key);

  @override
  State<StatefulWidget> createState() => StarItemFieldState();
}

class StarItemFieldState extends State<StarItemField> {

  Widget createItem(Color color){
    return GestureDetector(
      child: ImageIcon(
        widget.image,
        size: widget.size,
        color: color,
      ),
      onPanDown: (_){
        double x,y;
        x=_.localPosition.dx;
        y=_.localPosition.dy;
        double pos = x > widget.size * widget.fill ? widget.size : x;
        StarNotification(index: widget.index + pos / widget.size).dispatch(context);
      },
    );
  }

  Widget createStar(){
    double widthValue = widget.select * widget.size;
    return Stack(
      children: <Widget>[
        createItem(widget.unselected),
        ClipRect(
          clipper: StarClipper(width: widthValue),
          clipBehavior: Clip.hardEdge,
          child: createItem(widget.selected),
        )
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return createStar();
  }
}